import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart' hide Action;
import 'package:www.znpigai.teacher/global_store/state.dart';
import 'package:www.znpigai.teacher/utils/util.dart';

import 'action.dart';
import 'state.dart';

Widget buildView(
    SubjectEditState state, Dispatch dispatch, ViewService viewService) {
  return Scaffold(
    appBar: AppBar(
      title: new Text(
        "修改题目",
        style: TextStyle(color: Colors.black54, fontSize: 18),
      ),
      centerTitle: true,
      elevation: 2,
    ),
    body: SingleChildScrollView(
      child: Card(
        margin: EdgeInsets.fromLTRB(10, 10, 10, 80),
        color: Colors.white,
        elevation: 2,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(5.0),
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Padding(
                padding: EdgeInsets.only(left: 10),
                child: Row(
                  children: <Widget>[
                    Text(
                      '写作体裁',
                      style:
                          TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
                    ),
                    Spacer(),
                    SizedBox(
                      width: 60,
                      child: FlatButton(
                        child: Text('选择', style: TextStyle(color: Colors.blue)),
                        onPressed: () =>
                            dispatch(SubjectEditActionCreator.onEdit('type')),
                      ),
                    ),
                  ],
                )),
            Padding(
                padding: EdgeInsets.symmetric(horizontal: 10),
                child: state.subject.articleTypeList.isEmpty
                    ? Text('无')
                    : Wrap(
                        spacing: 10,
                        children:
                            state.subject.articleTypeList.map<Widget>((type) {
                          return Chip(label: Text(GlobalState.types[type]));
                        }).toList(),
                      )),
            Padding(
                padding: EdgeInsets.symmetric(vertical: 5),
                child: Divider(height: 1)),
            Padding(
                padding: EdgeInsets.only(left: 10),
                child: Row(
                  children: <Widget>[
                    Text('年级',
                        style: TextStyle(
                            fontSize: 16, fontWeight: FontWeight.w500)),
                    Spacer(),
                    SizedBox(
                        width: 60,
                        child: FlatButton(
                          child:
                              Text('选择', style: TextStyle(color: Colors.blue)),
                          onPressed: () => dispatch(
                              SubjectEditActionCreator.onEdit('grade')),
                        )),
                  ],
                )),
            Padding(
                padding: EdgeInsets.symmetric(horizontal: 10),
                child: state.subject.gradeList.isEmpty
                    ? Text('无')
                    : Wrap(
                        spacing: 10,
                        children: state.subject.gradeList.map<Widget>((type) {
                          return Chip(label: Text(GlobalState.grades[type]));
                        }).toList(),
                      )),
            Padding(
                padding: EdgeInsets.symmetric(vertical: 5),
                child: Divider(height: 1)),
            Padding(
                padding: EdgeInsets.only(left: 10),
                child: Row(
                  children: <Widget>[
                    Text('常考题型',
                        style: TextStyle(
                            fontSize: 16, fontWeight: FontWeight.w500)),
                    Spacer(),
                    SizedBox(
                        width: 60,
                        child: FlatButton(
                          child:
                              Text('选择', style: TextStyle(color: Colors.blue)),
                          onPressed: () => dispatch(
                              SubjectEditActionCreator.onEdit('tagList')),
                        )),
                  ],
                )),
            Padding(
                padding: EdgeInsets.symmetric(horizontal: 10),
                child: state.subject.tagList.isEmpty
                    ? Text('无')
                    : Wrap(
                        spacing: 10,
                        children: state.subject.tagList.map<Widget>((item) {
                          return Chip(label: Text(GlobalState.tags[item]));
                        }).toList(),
                      )),
            Padding(
                padding: EdgeInsets.symmetric(vertical: 5),
                child: Divider(height: 1)),
            Padding(
                padding: EdgeInsets.only(left: 10),
                child: Row(
                  children: <Widget>[
                    Text('作文主题',
                        style: TextStyle(
                            fontSize: 16, fontWeight: FontWeight.w500)),
                    Spacer(),
                    SizedBox(
                        width: 60,
                        child: FlatButton(
                          child:
                              Text('选择', style: TextStyle(color: Colors.blue)),
                          onPressed: () => dispatch(
                              SubjectEditActionCreator.onEdit('themeList')),
                        )),
                  ],
                )),
            Padding(
                padding: EdgeInsets.symmetric(horizontal: 10),
                child: state.subject.themeList.isEmpty
                    ? Text('无')
                    : Wrap(
                        spacing: 10,
                        children: state.subject.themeList.map<Widget>((item) {
                          return Chip(label: Text(GlobalState.themes[item]));
                        }).toList(),
                      )),
            Padding(
                padding: EdgeInsets.symmetric(vertical: 5),
                child: Divider(height: 1)),
            Padding(
                padding: EdgeInsets.only(left: 10),
                child: Row(
                  children: <Widget>[
                    Text(
                      '标题',
                      style:
                          TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
                    ),
                    Spacer(),
                    SizedBox(
                      width: 60,
                      child: FlatButton(
                        child: Text('编辑', style: TextStyle(color: Colors.blue)),
                        onPressed: () =>
                            dispatch(SubjectEditActionCreator.onEdit('title')),
                      ),
                    ),
                  ],
                )),
            Padding(
              padding: EdgeInsets.symmetric(horizontal: 10),
              child: state.subject.title.isEmpty
                  ? Text('无')
                  : Text(state.subject.title),
            ),
            Padding(
                padding: EdgeInsets.symmetric(vertical: 5),
                child: Divider(height: 1)),
            Padding(
                padding: EdgeInsets.only(left: 10),
                child: Row(
                  children: <Widget>[
                    Text('作文要求',
                        style: TextStyle(
                            fontSize: 16, fontWeight: FontWeight.w500)),
                    Spacer(),
                    SizedBox(
                        width: 60,
                        child: FlatButton(
                          child:
                              Text('编辑', style: TextStyle(color: Colors.blue)),
                          onPressed: () => dispatch(
                              SubjectEditActionCreator.onEdit('demand')),
                        )),
                  ],
                )),
            Padding(
                padding: EdgeInsets.only(left: 10),
                child: Offstage(
                  offstage: state.subject.demand.isEmpty,
                  child: Text(Util.plainText(state.subject.demand)),
                )),
            Padding(
                padding: EdgeInsets.symmetric(vertical: 5),
                child: Divider(height: 1)),
            Padding(
                padding: EdgeInsets.only(left: 10),
                child: Row(
                  children: <Widget>[
                    Text('中心词(空格分隔)',
                        style: TextStyle(
                            fontSize: 16, fontWeight: FontWeight.w500)),
                    Spacer(),
                    FlatButton(
                      child:
                          Text('编辑(可选)', style: TextStyle(color: Colors.blue)),
                      onPressed: () =>
                          dispatch(SubjectEditActionCreator.onEdit('keywords')),
                    ),
                  ],
                )),
            Padding(
                padding: EdgeInsets.symmetric(horizontal: 10),
                child: state.subject.keywordList.isEmpty
                    ? Text('无')
                    : Wrap(
                        spacing: 10,
                        children: state.subject.keywordList.map<Widget>((type) {
                          return Chip(label: Text(type));
                        }).toList(),
                      )),
            Padding(
                padding: EdgeInsets.symmetric(vertical: 5),
                child: Divider(height: 1)),
            Padding(
                padding: EdgeInsets.only(left: 10),
                child: Row(
                  children: <Widget>[
                    Text('构思点拨',
                        style: TextStyle(
                            fontSize: 16, fontWeight: FontWeight.w500)),
                    Spacer(),
                    FlatButton(
                      child:
                          Text('编辑(可选)', style: TextStyle(color: Colors.blue)),
                      onPressed: () =>
                          dispatch(SubjectEditActionCreator.onEdit('ideation')),
                    ),
                  ],
                )),
            Padding(
                padding: EdgeInsets.only(left: 10),
                child: Offstage(
                  offstage: state.subject.ideation.isEmpty,
                  child: Text(Util.plainText(state.subject.ideation)),
                )),
            SizedBox(height: 20),
          ],
        ),
      ),
    ),
    bottomSheet: Container(
      padding: EdgeInsets.all(10),
      width: double.infinity,
      color: Colors.black12,
      child: Row(
        children: <Widget>[
          Spacer(),
          FlatButton(
            padding: EdgeInsets.symmetric(horizontal: 40, vertical: 10),
            color: Colors.blue,
            textColor: Colors.white,
            child: Text('保存'),
            shape: RoundedRectangleBorder(
                side: BorderSide(
                  color: Colors.white,
                  width: 0,
                ),
                borderRadius: BorderRadius.circular(20)),
            onPressed: () {
              if (state.subject.title.isEmpty) {
                Util.alert('请填写标题', viewService.context);
              } else if (state.subject.demand.isEmpty) {
                Util.alert('请填写作文要求', viewService.context);
              } else {
                dispatch(SubjectEditActionCreator.onDone());
              }
            },
          ),
          Spacer(),
        ],
      ),
    ),
  );
}
